package com.zhledu.datastructure.List;

import java.text.NumberFormat;
import java.util.Scanner;

public class MyArrayList {
    private static final Integer MaxSize = 20;

    public static boolean initList(DataElem<Number> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入列表初始的长度：0到" + (MaxSize - 1));
        int len = scanner.nextInt();
        if (len < 0 || len > MaxSize - 1) {
            System.out.println("您输入的数据有误");
            return false;
        }
        list.data = new Number[MaxSize];
        for (int i = 0; i < len; i++) {
            list.data[i] = (int) (Math.random() * 100) + 1;
            list.length++;
        }

        return true;
    }

    public static void print(DataElem<Number> list) {
        System.out.println("遍历列表");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list.data[i] + "\t");
        }
        System.out.println();
    }

    public static boolean insert(DataElem<Number> list, int index, Number elem) {
        if (index < 0 || index > list.length) {
            System.out.println("插入的位置错误");
            return false;
        } else {
            for (int j = list.length - 1; j >= index; j--) {
                list.data[j + 1] = list.data[j];
            }
            list.data[index] = elem;
            list.length++;
            return true;
        }
    }

    public static int find(DataElem<Number> list, Number elem) {
        for (int i = 0; i < list.length; i++) {
            if (list.data[i].doubleValue()==elem.doubleValue()) {
                return i;
            }
        }
        return -1;
    }

    public static void sort(DataElem<Number> list, int type) {
        int index = 0;
        Number elem;
        for (int i = 0; i < list.length - 1; i++) {
            index = i;
            elem = list.data[i];
            for (int j = i + 1; j < list.length; j++) {
                if (type == 1) {
                    if (elem.doubleValue() >= list.data[j].doubleValue()) {
                        elem = list.data[j];
                        index = j;
                    }
                } else if (type == -1) {
                    if (elem.doubleValue() <= list.data[j].doubleValue()) {
                        elem = list.data[j];
                        index = j;
                    }
                }
            }
            if (index != i) {
                Number temp = list.data[i];
                list.data[i] = list.data[index];
                list.data[index] = temp;
            }
        }
    }


    public static Number delete(DataElem<Number> list, int index) {
        if (index < 0 || index > list.length) {
            System.out.println("删除的位置错误");
            return null;
        } else {
            Number elem = list.data[index];
            for (int j = index; j < list.length-1; j++) {
                list.data[j] = list.data[j + 1];
            }
            list.length--;
            return elem;
        }
    }


    public static void main(String[] args) throws Exception {
        DataElem<Number> list = new DataElem<>();
        initList(list);
        print(list);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要插入元素的位置和元素的值，用空格隔开：");
        int index = scanner.nextInt();
        Number elem = NumberFormat.getInstance().parse(scanner.next());
        insert(list, index, elem);
        print(list);
        System.out.println("请输入你要查找的元素：");
         elem = NumberFormat.getInstance().parse(scanner.next());
        index= find(list,elem);
        System.out.println("您要找的元素"+elem+"的下标是："+index);
        System.out.println("请输入你要删除元素的位置：");
        index = scanner.nextInt();
        elem = delete(list, index);
        System.out.println(index + "处删除的元素为：" + elem);
        print(list);
        System.out.println("请输入您要排序的类型，1为升序，-1为降序");
        int sort=scanner.nextInt();
        sort(list, sort);
        print(list);
    }


}
